package com.ruoyi.web.controller.personnel;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.personnel.domain.FamilyRelations;
import com.ruoyi.personnel.domain.vo.FamilyRelationsVo;
import com.ruoyi.personnel.service.IFamilyRelationsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 家庭关系（与人员信息关联）Controller
 *
 * @author ruoyi
 * @date 2021-02-23
 */
@Api("家庭关系（与人员信息关联）")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/system/relations" )
public class FamilyRelationsController extends BaseController {

    private final IFamilyRelationsService iFamilyRelationsService;

    /**
     * 查询家庭关系（与人员信息关联）列表
     */
    @ApiOperation("查询家庭关系（与人员信息关联）列表")
    @PreAuthorize("@ss.hasPermi('system:relations:list')")
    @GetMapping("/list")
    public TableDataInfo list(FamilyRelations familyRelations) {
        startPage();
        List<FamilyRelations> list = iFamilyRelationsService.queryList(familyRelations);
        return getDataTable(list);
    }

    /**
     * 导出家庭关系（与人员信息关联）列表
     */
    @ApiOperation("导出家庭关系（与人员信息关联）列表")
    @PreAuthorize("@ss.hasPermi('system:relations:export')" )
    @Log(title = "家庭关系（与人员信息关联）" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(FamilyRelations familyRelations) {
        List<FamilyRelations> list = iFamilyRelationsService.queryList(familyRelations);
        ExcelUtil<FamilyRelations> util = new ExcelUtil<FamilyRelations>(FamilyRelations.class);
        return util.exportExcel(list, "relations" );
    }

    /**
     * 获取家庭关系（与人员信息关联）详细信息
     */
    @ApiOperation("获取家庭关系（与人员信息关联）详细信息")
    @PreAuthorize("@ss.hasPermi('system:relations:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) String id) {
        return AjaxResult.success(iFamilyRelationsService.getById(id));
    }

    /**
     * 新增家庭关系（与人员信息关联）
     */
    @ApiOperation("新增家庭关系（与人员信息关联）")
    @PreAuthorize("@ss.hasPermi('system:relations:add')" )
    @Log(title = "家庭关系（与人员信息关联）" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FamilyRelationsVo familyRelationsVo) {

      List<FamilyRelations> familyRelationsList = familyRelationsVo.getFamilyRelationsList();

      for (FamilyRelations familyRelations : familyRelationsList) {
        familyRelations.setCreateBy(SecurityUtils.getUsername());
      }

      return toAjax(iFamilyRelationsService.saveBatch(familyRelationsList) ? 1 : 0);
    }

    /**
     * 修改家庭关系（与人员信息关联）
     */
    @ApiOperation("修改家庭关系（与人员信息关联）")
    @PreAuthorize("@ss.hasPermi('system:relations:edit')" )
    @Log(title = "家庭关系（与人员信息关联）" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FamilyRelationsVo familyRelationsVo) {
      List<FamilyRelations> familyRelationsList = familyRelationsVo.getFamilyRelationsList();
      return toAjax(iFamilyRelationsService.updateBatchById(familyRelationsList) ? 1 : 0);
    }

    /**
     * 删除家庭关系（与人员信息关联）
     */
    @ApiOperation("删除家庭关系（与人员信息关联）")
    @PreAuthorize("@ss.hasPermi('system:relations:remove')" )
    @Log(title = "家庭关系（与人员信息关联）" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(iFamilyRelationsService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }

}
